package com.accp.action;

import com.accp.biz.ComputerBiz;
import com.accp.pojo.Computer;
import com.accp.pojo.Users;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;


import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

public class ComputerAction implements ModelDriven<Computer> {


    private ComputerBiz computerBiz;

    private Computer computer = new Computer();

    @Override
    public Computer getModel() {
        return computer;
    }

    public void setComputerBiz(ComputerBiz computerBiz) {
        this.computerBiz = computerBiz;
    }

    /**
     * 分页查询电脑信息
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public String selectComputer() {
        Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
        try {
            List list = computerBiz.selectByPage("Computer", 1, 3);
            int totalPage = computerBiz.searchRecordsNextPage(3, "Computer");
            request.put("list", list);
            request.put("totalPage", totalPage);
            request.put("currentPage", 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "selectComputer";
    }

    /**
     * 分页查找下一页
     * @return
     */
    @SuppressWarnings("rawtypes")
    public String selectNextPage(){
        HttpServletRequest request = ServletActionContext.getRequest();
        int next = Integer.parseInt(request.getParameter("page"));
        try{
            List list = computerBiz.selectByPage("Computer", next, 3);
            int total = computerBiz.searchRecordsNextPage(3, "Computer");
            request.setAttribute("list", list);
            request.setAttribute("currentPage", next);
            request.setAttribute("totalPage", total);
        }catch(Exception e){
            e.printStackTrace();
        }
        return "selectNextPage";
    }
    /**
     * 删除信息
     */
    public String delete() {
        HttpServletRequest request = ServletActionContext.getRequest();
        String id = request.getParameter("id");
        if (computerBiz.delete(Integer.parseInt(id))) {
            return "delete";
        }
        return "error";
    }
    /**
     * 添加
     */
    public String add(){
        computerBiz.add(computer);
        return "add";
    }

    /**
     * 跳转修改页面
     */
    public String reupdate(){
        Map<String,Object> req = (Map<String, Object>) ActionContext.getContext().get("request");
        HttpServletRequest request = ServletActionContext.getRequest();
        Integer id = Integer.valueOf(request.getParameter("id"));
        computer=computerBiz.findById(id);
        req.put("computer", computer);
        return "reupdate";
    }
    /**
     *修改保存
     */
    public String update(){
        Map<String,Object> session = ActionContext.getContext().getSession();
        Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
        Computer computers = new Computer ();
        computers.setId(computer.getId());
        computers.setDname(computer.getDname());
        computers.setColor(computer.getColor());
        computers.setCreat_time(computer.getCreat_time());
        computers.setShell_time(computer.getShell_time());
        computers.setPicture(computer.getPicture());
        boolean flag = computerBiz.update(computers);
        if(!flag){
            request.put("message", "修改成功");
            return "update";
        }
        return "error";
    }


}
